<?
    class MenuItem {
        private $_allow = array();
        private $_title = '';
        private $_link = '';
        private $_linkisaction = false;
        
        function __construct($title,$link,$permissionlevel = AuthConstants::EVERYONE, $linkisaction = false) {
            $this->_title = $title;
            $this->_link = $link;
            $this->_linkisaction = $linkisaction;
            
            if (is_array($permissionlevel))
                $this->_allow = $permissionlevel;
            else
                $this->_allow[] = $permissionlevel;
        }
        function GetTitle() { return $this->_title; }
        function RenderLink() {
            if (AuthManager::AuthLevelMatches($this->_allow)) {
                return "<a " . ($this->_linkisaction ? "onClick" : "href") . "='{$this->_link}'>{$this->_title}</a>\n";
            }
            else return null;
        }
    }
    
    class Menu {
        private $_subitems = array();
        private $_title = '';
        
        function __construct($title) { $this->_title = $title; }
        function AddItem($item) {
            $this->_subitems[] = $item;
        }
        function GetTitle() { return $this->_title; }
        function Items() { 
            return $this->_subitems; 
        }
    }
    
    class MenuManager {
        private static $_menus = array();
        static function AddMenu($page,$menu) {
            self::$_menus[$page][] = $menu;
        }
        static function HasMenu($pagetitle) {
            return (self::$_menus[$pagetitle] ? true : false);
        }
        static function RenderMenu($pagetitle) {
            if (self::$_menus[$pagetitle]) {
                $buffer = "";
                $itemcount = 0;
                foreach (self::$_menus[$pagetitle] as $menu) {
                    $buffer .= "<span class='section_header'>" . $menu->GetTitle() . "</span>";
                    $buffer .= "<span class='section_content'>";
                    foreach ($menu->Items() as $item) {
                        $stuff = $item->RenderLink();
                        if ($stuff != null) {
                            $buffer .= $stuff . '<br>';
                            $itemcount++;
                        }
                    }
                    $buffer .= "</span>";
                    if ($itemcount > 0) echo $buffer;
                }
            }
            else return ''; // then the menu should not be shown
        }
        static function GetMainMenu() {
            // auth constants are defined in /include/constants.php
            $menu = new Menu("Main Menu");
            $menu->AddItem(new MenuItem("Home",                 "index.php",                    AuthConstants::EVERYONE));
            $menu->AddItem(new MenuItem("Guest and Landlord Login",                "login.php",                    AuthConstants::NOT_LOGGED_IN));
            $menu->AddItem(new MenuItem("RPI Student Login",                "login.php?method=cas",                    AuthConstants::NOT_LOGGED_IN));
           
            $newguestaccts = DBM::CountRows("SELECT * FROM guests WHERE approved=0;");
            $menu->AddItem(new MenuItem("Guest Accounts" . ($newguestaccts > 0 ? " <b>( $newguestaccts new )</b>" : ""),"admin_guest.php",AuthConstants::ADMINISTRATORS));
            
            $menu->AddItem(new MenuItem("Messages",             "admin_messages.php",           AuthConstants::ADMINISTRATORS));
            $menu->AddItem(new MenuItem("Listing Structure",    "admin_listingstructure.php",   AuthConstants::ADMINISTRATORS));
            $menu->AddItem(new MenuItem("New Listing",          "listing.php",                  array(AuthConstants::ADMINISTRATORS,AuthConstants::LANDLORDS)));
            $menu->AddItem(new MenuItem("Favorites",            "favorites.php",                array(AuthConstants::GUESTS,AuthConstants::STUDENTS,AuthConstants::ADMINISTRATORS)));
            $menu->AddItem(new MenuItem("Settings",             "admin_settings.php",           AuthConstants::ADMINISTRATORS));
            $menu->AddItem(new MenuItem("Logs",                 "log.php",                      AuthConstants::ADMINISTRATORS));
            $menu->AddItem(new MenuItem("Edit My Profile",           "profile.php?action=edit",      AuthConstants::LANDLORDS));
            $menu->AddItem(new MenuItem("Edit My Listings",     "listings.php?action=edit",     AuthConstants::LANDLORDS));
            
            /*
            if (AuthManager::CheckPermissions(AuthConstants::ALL_LOGGED_IN)) {
                $newmessages = Conversation::CountUnreadMessages();
                $menu->AddItem(new MenuItem("Read Messages" . ($newmessages > 0 ? " <b>( $newmessages new )</b>" : ""),        "messages.php",                 AuthConstants::ALL_LOGGED_IN));
            }
            */
            $menu->AddItem(new MenuItem("Browse",      "browse.php",                   array(AuthConstants::ALL_LOGGED_IN)));
            $menu->AddItem(new MenuItem("Search",      "search.php",                   array(AuthConstants::ALL_LOGGED_IN)));
            $menu->AddItem(new MenuItem("Change Password",      "changepassword.php",        array(AuthConstants::GUESTS, AuthConstants::LANDLORDS, AuthConstants::ADMINISTRATORS)));
            $menu->AddItem(new MenuItem("Contact Us",           "contact.php",                  AuthConstants::EVERYONE));
            $menu->AddItem(new MenuItem("FAQ",                  "faq.php",                      AuthConstants::EVERYONE));
            $menu->AddItem(new MenuItem("Logout",               "logout.php",                   AuthConstants::ALL_LOGGED_IN));
            
            $arr = array();
            foreach ($menu->Items() as $item) {
                $link = $item->RenderLink();
                if ($link != null) $arr[] = $link;
            }
            return implode(" &#9679; ",$arr);
        }   
    }
    
    /* Build the menus on the pages and add them to the MenuManaager */
?>
